package com.zr.lagalplant.controller;

import com.zr.lagalplant.pojo.LegalPlantAddVo;
import com.zr.lagalplant.pojo.LegalPlantSelectVo;
import com.zr.lagalplant.pojo.LegalPlantStatusVo;
import com.zr.lagalplant.pojo.LegalPlantUpdateVo;
import com.zr.lagalplant.service.LegalPersonService;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Administrator on 2019/10/8.
 */
@CrossOrigin //支持跨域-
@RestController
public class LegalPersonController {
    @Autowired
    private LegalPersonService legalPersonService;

    //分页查询
    @PostMapping("legalPlant/query")
    public ResultVo query(@RequestBody @Valid LegalPlantSelectVo legalPlantSelectVo){
        ResultVo resultVo=legalPersonService.querypage(legalPlantSelectVo);
        return resultVo;
    }

    //法人下拉框
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo getLegalPerson(){
        Integer userId=1;
        ResultVo resultVo =  legalPersonService.queryLegalPersonByUserId(userId);
        return resultVo;
    }

    //新增
    @PostMapping("legalPlant/add")
    public ResultVo add(@RequestBody @Valid LegalPlantAddVo legalPlantAddVo, BindingResult bindingResult){
        //验证是否存在错误
        //有错
        if(bindingResult.hasErrors()){
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //没错
        ResultVo resultVo=legalPersonService.add(legalPlantAddVo);
        return resultVo;

    }
//    修改
    @PostMapping("legalPlant/update")
    public ResultVo update(@RequestBody @Valid LegalPlantUpdateVo legalPlantUpdateVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()) {
            return ResultVoBuilder.error("500", bindingResult.getFieldError().getDefaultMessage());
        }

        return legalPersonService.updateLegalPlant(legalPlantUpdateVo);
    }

    //修改状态

    @PostMapping("legalPlant/updatestatus")
    public ResultVo updateStatus(@RequestBody @Valid LegalPlantStatusVo legalPlantStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVoBuilder.error("500", bindingResult.getFieldError().getDefaultMessage());
        }

        return legalPersonService.updateStatus(legalPlantStatusVo);
    }

}
